package com.zhiwang.planet.ui.adapter

import android.annotation.SuppressLint
import android.graphics.drawable.LevelListDrawable
import android.text.Spannable
import android.text.SpannableString
import android.text.style.AbsoluteSizeSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.zhiwang.planet.R
import com.zhiwang.planet.mvvm.model.entity.VipItem
import com.zhiwang.planet.util.sp2px
import kotlinx.android.synthetic.main.item_vip_type_layout.view.*

class VipChooseAdapter : androidx.recyclerview.widget.RecyclerView.Adapter<VipChooseAdapter.ItemViewHolder>() {

    var layoutInflater: LayoutInflater? = null
    var vipModels = ArrayList<VipItem>()
    var recyclerView: androidx.recyclerview.widget.RecyclerView? = null
    lateinit var itemChosenListener: OnItemChosenListener
    var prev: Int = -1
    var curr: Int = -1

    fun setVipModels(models: List<VipItem>) {
        this.vipModels.clear()
        this.vipModels.addAll(models)
        notifyDataSetChanged()
    }

    val itemClickListener = View.OnClickListener {
        val clickedPosition = recyclerView!!.getChildAdapterPosition(it)
        prev = curr
        curr = clickedPosition
        notifyItemChanged(prev)
        notifyItemChanged(curr)
        itemChosenListener.onItemChosen(clickedPosition)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        if (layoutInflater == null) {
            layoutInflater = LayoutInflater.from(parent.context)
            recyclerView = parent as androidx.recyclerview.widget.RecyclerView
        }
        return ItemViewHolder(layoutInflater!!.inflate(R.layout.item_vip_type_layout, parent, false))
    }

    override fun getItemCount() = vipModels.size

    override fun onBindViewHolder(viewHolder: ItemViewHolder, position: Int) {
        viewHolder.bind()
    }

    inner class ItemViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {
        var vipPrice: TextView = itemView.vip_money
        var vipPeriod: TextView = itemView.vip_period
        var vipName: TextView = itemView.vip_name
        var suggestedImage: ImageView = itemView.suggsted_image

        @SuppressLint("SetTextI18n")
        fun bind() {
            itemView.setOnClickListener(itemClickListener)
            //data binding
            val vipItem = vipModels[adapterPosition]
            val price = SpannableString("¥${vipItem.cost}")
            price.setSpan(AbsoluteSizeSpan(sp2px(itemView.context, 11f)), 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
            price.setSpan(AbsoluteSizeSpan(sp2px(itemView.context, 18f)), 1, price.length, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
            vipPrice.text = price
            vipPeriod.text = "（${vipItem.period_days}天）"
            vipName.text = vipItem.vip_txt
            suggestedImage.visibility = if (vipItem.isSuggested) View.VISIBLE else View.INVISIBLE

            if (vipItem.vip_txt == "SVIP")
                itemView.setBackgroundResource(R.drawable.svip_selector)

            if (vipItem.vip_txt == "金卡VIP")
                itemView.setBackgroundResource(R.drawable.vip_selector)

            //背景色
            if (adapterPosition == curr) {

                (itemView.background as LevelListDrawable).level = 1

            } else (itemView.background as LevelListDrawable).level = 0

        }
    }

    interface OnItemChosenListener {
        fun onItemChosen(index: Int)
    }
}